 // Fretta Template Gamemode
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "cl_spawnmenu.lua" )
include( "shared.lua" )
include( "sql_database.lua" )
include( "do_work.lua" )

// send these items to the client
resource.AddFile("models/weapons/w_fists_t.mdl")
resource.AddFile("models/weapons/w_fists_t.vvd")
resource.AddFile("models/weapons/w_fists_t.phy")
resource.AddFile("models/weapons/w_fists_t.sw.vtx")
resource.AddFile("models/weapons/w_fists_t.dx90.vtx")
resource.AddFile("models/weapons/w_fists_t.dx80.vtx")
resource.AddFile("models/weapons/v_punch.mdl")
resource.AddFile("models/weapons/v_punch.vvd")
resource.AddFile("models/weapons/v_punch.sw.vtx")
resource.AddFile("models/weapons/v_punch.dx90.vtx")
resource.AddFile("models/weapons/v_punch.dx80.vtx")
resource.AddFile("sound/weapons/fists/hl2_slash1.wav")
resource.AddFile("sound/weapons/fists/hl2_slash2.wav")
-- required silk icons
resource.AddFile( "gui/silkicons/building.vmt" )
resource.AddFile( "gui/silkicons/wrench_orange.vmt" )
resource.AddFile( "gui/silkicons/money.vmt" )

---------------------------------------------------------
--   Name: gamemode:PlayerInitialSpawn( Player ply )
--   Desc: Our very first spawn in the game.
---------------------------------------------------------
function GM:PlayerInitialSpawn( player )

	player:SetTeam( TEAM_UNASSIGNED )
	player:SetPlayerClass( "Spectator" )
	player.m_bFirstSpawn = true
	player:UpdateNameColor()
	
	GAMEMODE:CheckPlayerReconnected( player )

	player.AllowWeaponPickupFix = 1

	timer.Create(player:SteamID().."_workTimer", 5, 0, function() timer.Stop(player:SteamID().."_workTimer") end)
end

local function StopTimers( player )
	timer.Destroy(player:SteamID().."_workTimer")
end

function DropCurrentWeapon(player)
	local CurrentWeapon = player:GetActiveWeapon()
	local NewWeapon = ents.Create(CurrentWeapon:GetClass())
	local currentWeaponClip1 = CurrentWeapon:Clip1()
	local currentWeaponClip2 = CurrentWeapon:Clip2()
	
	
	if CurrentWeapon:GetClass() != "hl2_combo_fists" && CurrentWeapon:GetClass() != "weapon_physcannon" && CurrentWeapon:GetClass() != "weapon_physgun" then
		player:StripWeapon(CurrentWeapon:GetClass())	
		NewWeapon:SetPos(player:GetShootPos() + (player:GetAimVector() * 30))
	
		player.AllowWeaponPickupFix = 0
	
		timer.Simple(1.5,PickupDelayFunc, player)
	
		NewWeapon:Spawn()
	
		NewWeapon:SetClip1(currentWeaponClip1)
		NewWeapon:SetClip2(currentWeaponClip2)
	end
end

function SpawnProp(player, command, arguments)
	if ( arguments[1] == nil ) then return end
	if ( !util.IsValidModel( arguments[1] ) ) then return end
	
	local iSkin = arguments[2] or 0

	if ( util.IsValidProp( arguments[1] ) ) then 
		local tracetable = {}
		tracetable.start = player:GetShootPos()
		local tempVector = player:GetAimVector()*256
		tracetable.endpos = player:GetShootPos() + tempVector
		tracetable.filter = MASK_NPCWORLDSTATIC
		local prop = ents.Create("prop_physics")
		if (!prop:IsValid()) then return end
		prop:SetPos((util.TraceLine(tracetable)).HitPos + Vector(0,0,35))
		prop:SetModel(arguments[1])
		prop:Spawn()
		cleanup.Add(player, "props", prop)
		undo.Create("spawnedprop")
		undo.AddEntity(prop)
		undo.SetPlayer(player)
		undo.Finish()
		local phys = prop:GetPhysicsObject()
		if phys:IsValid() then
			phys:EnableMotion(false)
		else
			return
		end

		prop:Activate()
		prop:SetNWString("Owner", player:Name())
	end
end

function PickupDelayFunc(player)
	player.AllowWeaponPickupFix = 1
end

function RePickupFix(player, weapon)
	if player.AllowWeaponPickupFix == 0 then
		return false
	end
end

hook.Add("PlayerCanPickupWeapon", "FixForPickup", RePickupFix)
hook.Add("PlayerDisconnected", "StopTimers", StopTimers)

concommand.Add("DropWeapon", DropCurrentWeapon)
concommand.Add("ks_spawn", SpawnProp )
concommand.Add("ks_SaveStats", saveStats)

